const hasOwn = {}.hasOwnProperty;

function login(that, ops) {
	return new Promise((resolve, reject) => {
		uni.getProvider({
			service: 'oauth',
			success: function(res) {
				console.log(res.provider);
				//支持微信、qq和微博等
				if (~res.provider.indexOf('weixin')) {
					uni.login({
						provider: "weixin",
						success: res => {
							console.log(res);
							that.$toast("登录成功");
							that.$store.dispatch("requestLogin", {
								code: res.code,
								platform: 'mp-weixin',
								options: ops
							}).then(v => {
								if (v.status === 1) {
									that.globalData = that._plugins.extend(
										getApp()
										.globalData, {
											token: v.data.token,
											userInfo: v.data.userInfo,
											openID: v.data.openID
										});
									that._plugins.extend(that.$store.globalData,
										that
										.globalData);
									uni.setStorageSync('globalData', JSON
										.stringify(getApp()
											.globalData));

									resolve(that.globalData)
								} else {
									that.$toast("获取openid失败")
									console.log(v)
									reject(v)
								}
							}).catch(v => {
								that.$toast("获取openid失败")
								console.log(v)
								reject(v)
							})
						},
						fail: res => {
							console.log(res)
							that.$toast("登录失败");
							reject(res)
						}
					})
				} else {
					that.$toast("登录失败");
					reject(res)
				}
			},
			fail(v) {
				that.$toast("登录失败");
				reject(v)
			}
		})
	})

}

function verifyToken(that, token) {
	that.$store.dispatch("verifyLoginToken", {token:token}).then(v => {
		if (v.status === 1) {
			if (!v.data.isValid) {
				login(that)
			}
		} else {
			console.log(v)
		}
	}).catch(v => {
		console.log(v)
	})
}


export const common = {
	boolLogin(){
		let globalData = uni.getStorageSync("globalData");
		globalData && (globalData = JSON.parse(globalData));
		let token = globalData.token;
		return new Promise((resolve, reject) =>{
			token ? resolve() : reject()
		})
	},
	toggleLang(that) {
		let lang = 'zh';
		try {
			const res = uni.getSystemInfoSync();
			lang = res.language;
		} catch (e) {}

		if (lang.indexOf('en') !== -1) {
			that.$i18n.locale = 'en-US';
		}
		if (lang.indexOf('zh') !== -1) {
			that.$i18n.locale = 'zh-CN';
		}
		this.setTabBarItem(that)
	},
	setTabBarItem(that) {
		uni.setTabBarItem({
			index: 0,
			text: that.$t('index')['首页']
		});
		uni.setTabBarItem({
			index: 1,
			text: that.$t('index')['发现']
		});
		uni.setTabBarItem({
			index: 2,
			text: that.$t('index')['消息']
		});
		uni.setTabBarItem({
			index: 3,
			text: that.$t('index')['我的']
		});
	},
	login(that) {
		return login(that);
	},
	miniappToken(target) {
		let that = target;
		let globalData = uni.getStorageSync('globalData');
		globalData && (globalData = JSON.parse(globalData));
		//console.log(globalData)
		/* if (globalData && !globalData.token || !globalData) {
			login(that);
			//return false;
		} else */
		if (globalData) {
			that._plugins.extend(that.$store.globalData, that._plugins.extend(that.globalData, globalData));
			//verifyToken(that, globalData.token);
			//return globalData;
		}
	},
	returnJudgmentTypeData(v, a) {
		if (a === "SUCCESS") {
			switch (v) {
				case 'SUCCESS':
					return '已经达成';
				case 'FAILD':
					return '未达成';
				case 'PUSHON':
					return '促进中';
				default:
					return '状态未知';
			}
		} else {
			return "审核未通过"
		}

	},
	returnMessageTypeData(v) {
		switch (v) {
			case 'SYSMESSAGE':
				return '系统';
			default:
				return '未知';
		}
	},
	isArray(obj) {
		return (Array.isArray || _instanceOf(Array))(obj) && typeof obj !== "string";
	},
	isEmpty(v) {
		return typeof v === 'string' && v.replace(/^\s+|\s+$/gim, "") === "";
	},
	isTelphoneCode(v) {
		return /^1[3-9]\d{9}$/.test(v)
	},
	isEmptyObject(obj) {
		for (var name in obj) {
			return false;
		}
		return true;
	},
	isPlainObject(obj) {
		var key;
		if (!obj || typeof obj !== "object" || obj.nodeType) {
			return false;
		}
		try {
			if (obj.constructor &&
				!hasOwn.call(obj, "constructor") &&
				!hasOwn.call(obj.constructor.prototype, "isPrototypeOf")) {
				return false;
			}
		} catch (e) {
			return false;
		}
		for (key in obj) {}
		return key === undefined || hasOwn.call(obj, key);
	},
	isPassword(v) {
		return /[A-Z]{1,1}[a-z0-9]{7,7}/.test(v) ? true : false;
	},
	extend(a, b) {
		a = a || {};
		for (let n in b) a[n] = b[n]
		return a;
	},
	toast(title) {
		uni.showToast({
			title: title,
			icon: 'none'
		})
	}
}
